package com.huawei.hms.support.api.hwid;

import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;
import android.text.TextUtils;
import com.huawei.hms.api.Api.ApiOptions.HasOptions;
import com.huawei.hms.auth.api.signin.HuaweiIdSignInOptionsExtension;
import com.huawei.hms.support.api.entity.auth.PermissionInfo;
import com.huawei.hms.support.api.entity.auth.Scope;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.LOCALPERMISSION;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.PERMISSION;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.SCOPE;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class HuaweiIdSignInOptions implements Parcelable, HasOptions {
    public static final Creator<HuaweiIdSignInOptions> CREATOR = new Creator<HuaweiIdSignInOptions>() {
        public /* synthetic */ Object createFromParcel(Parcel parcel) {
            return a(parcel);
        }

        public /* synthetic */ Object[] newArray(int i) {
            return a(i);
        }

        public HuaweiIdSignInOptions a(Parcel parcel) {
            return new HuaweiIdSignInOptions(parcel);
        }

        public HuaweiIdSignInOptions[] a(int i) {
            return new HuaweiIdSignInOptions[i];
        }
    };
    public static final HuaweiIdSignInOptions DEFAULT_GAMES_SIGN_IN = new Builder().requestScopes(SCOPE_GAMES, new Scope[0]).build();
    public static final HuaweiIdSignInOptions DEFAULT_SIGN_IN = new Builder().requestId().requestProfile().build();
    public static final Scope SCOPE_GAMES = new Scope(SCOPE.SCOPE_ACCOUNT_GAME);
    private static final PermissionInfo a = new PermissionInfo().setPermissionUri(PERMISSION.UID);
    private static final Scope b = new Scope(SCOPE.SCOPE_ACCOUNT_PROFILE);
    private static final Scope c = new Scope(SCOPE.SCOPE_ACCOUNT_EMAIL);
    private static final Scope d = new Scope(SCOPE.SCOPE_ACCOUNT_OPENID);
    private final ArrayList<Scope> e;
    private ArrayList<PermissionInfo> f;

    public static final class Builder {
        private Set<Scope> a = new HashSet();
        private Set<PermissionInfo> b = new HashSet();

        public Builder(HuaweiIdSignInOptions huaweiIdSignInOptions) {
            this.a.addAll(huaweiIdSignInOptions.getScopeList());
            this.b.addAll(huaweiIdSignInOptions.getPermissionInfos());
        }

        public Builder requestUid() {
            this.b.add(HuaweiIdSignInOptions.a);
            return this;
        }

        public Builder requestServerAuthCode() {
            PermissionInfo permissionInfo = new PermissionInfo();
            permissionInfo.setPermissionUri(LOCALPERMISSION.SERVICE_AUTH_CODE);
            this.b.add(permissionInfo);
            return this;
        }

        @Deprecated
        public Builder requestAccessToken() {
            PermissionInfo permissionInfo = new PermissionInfo();
            permissionInfo.setPermissionUri(LOCALPERMISSION.ACCESS_TOKEN);
            this.b.add(permissionInfo);
            return this;
        }

        public Builder requestScopes(Scope scope, Scope... scopeArr) {
            this.a.add(scope);
            if (scopeArr != null && scopeArr.length > 0) {
                this.a.addAll(Arrays.asList(scopeArr));
            }
            return this;
        }

        public HuaweiIdSignInOptions build() {
            return new HuaweiIdSignInOptions(this.a, this.b);
        }

        public Builder addExtension(HuaweiIdSignInOptionsExtension huaweiIdSignInOptionsExtension) {
            return this;
        }

        public Builder requestEmail() {
            requestScopes(HuaweiIdSignInOptions.c, new Scope[0]);
            return this;
        }

        public Builder requestId() {
            return requestScopes(HuaweiIdSignInOptions.d, new Scope[0]);
        }

        public Builder requestIdToken(String str) {
            this.b.add(new PermissionInfo().setPermissionUri(PERMISSION.IDTOKEN));
            return this;
        }

        public Builder requestProfile() {
            requestScopes(HuaweiIdSignInOptions.b, new Scope[0]);
            return this;
        }

        public Builder setAccountName(String str) {
            return this;
        }

        public Builder setHostedDomain(String str) {
            return this;
        }

        @Deprecated
        public Builder requestServerAuthCode(String str) {
            return requestServerAuthCode(str, false);
        }

        @Deprecated
        public Builder requestServerAuthCode(String str, boolean z) {
            return requestServerAuthCode();
        }

        public Builder requestShippingAddress() {
            requestAccessToken();
            requestId();
            return requestScopes(new Scope(SCOPE.SCOPE_ACCOUNT_SHIPPING_ADDRESS), new Scope[0]);
        }
    }

    public HuaweiIdSignInOptions(Set<Scope> set, Set<PermissionInfo> set2) {
        this(new ArrayList(set), new ArrayList(set2));
    }

    public HuaweiIdSignInOptions(ArrayList<Scope> arrayList, ArrayList<PermissionInfo> arrayList2) {
        this.e = arrayList;
        this.f = arrayList2;
    }

    public HuaweiIdSignInOptions(Parcel parcel) {
        this.e = parcel.createTypedArrayList(Scope.CREATOR);
        this.f = parcel.createTypedArrayList(PermissionInfo.CREATOR);
    }

    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeList(this.e);
        parcel.writeList(this.f);
    }

    public int describeContents() {
        return 0;
    }

    public List<Scope> getScopeList() {
        return this.e;
    }

    public List<PermissionInfo> getPermissionInfos() {
        return this.f;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof HuaweiIdSignInOptions)) {
            return false;
        }
        HuaweiIdSignInOptions huaweiIdSignInOptions = (HuaweiIdSignInOptions) obj;
        if (a(this.e, huaweiIdSignInOptions.e) && a(this.f, huaweiIdSignInOptions.f)) {
            return true;
        }
        return false;
    }

    private <T> boolean a(ArrayList<T> arrayList, ArrayList<T> arrayList2) {
        if (arrayList == arrayList2) {
            return true;
        }
        if (arrayList.size() != arrayList2.size()) {
            return false;
        }
        return arrayList.containsAll(arrayList2);
    }

    public String toJson() throws JSONException {
        return toJsonObject().toString();
    }

    public JSONObject toJsonObject() throws JSONException {
        JSONArray jSONArray;
        Iterator it;
        JSONObject jSONObject = new JSONObject();
        if (this.e != null) {
            jSONArray = new JSONArray();
            it = this.e.iterator();
            while (it.hasNext()) {
                jSONArray.put(a((Scope) it.next()));
            }
            jSONObject.put("scopeArrayList", jSONArray);
        }
        if (this.f != null) {
            jSONArray = new JSONArray();
            it = this.f.iterator();
            while (it.hasNext()) {
                jSONArray.put(a((PermissionInfo) it.next()));
            }
            jSONObject.put("permissionArrayList", jSONArray);
        }
        return jSONObject;
    }

    public static HuaweiIdSignInOptions fromJson(String str) throws JSONException {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        return fromJsonObject(new JSONObject(str));
    }

    public static HuaweiIdSignInOptions fromJsonObject(JSONObject jSONObject) throws JSONException {
        int i = 0;
        JSONArray jSONArray = jSONObject.getJSONArray("scopeArrayList");
        ArrayList arrayList = new ArrayList();
        if (jSONArray != null) {
            for (int i2 = 0; i2 < jSONArray.length(); i2++) {
                arrayList.add(a(jSONArray.getJSONObject(i2)));
            }
        }
        JSONArray jSONArray2 = jSONObject.getJSONArray("permissionArrayList");
        ArrayList arrayList2 = new ArrayList();
        if (jSONArray2 != null) {
            while (i < jSONArray2.length()) {
                arrayList2.add(b(jSONArray2.getJSONObject(i)));
                i++;
            }
        }
        return new HuaweiIdSignInOptions(arrayList, arrayList2);
    }

    private JSONObject a(Scope scope) throws JSONException {
        JSONObject jSONObject = new JSONObject();
        if (scope.getScopeUri() != null) {
            jSONObject.put("mScopeUri", scope.getScopeUri());
        }
        return jSONObject;
    }

    private static Scope a(JSONObject jSONObject) {
        return new Scope(jSONObject.optString("mScopeUri", null));
    }

    private JSONObject a(PermissionInfo permissionInfo) throws JSONException {
        JSONObject jSONObject = new JSONObject();
        if (permissionInfo.getPermission() != null) {
            jSONObject.put("permission", permissionInfo.getPermission());
        }
        return jSONObject;
    }

    private static PermissionInfo b(JSONObject jSONObject) {
        return new PermissionInfo().setPermissionUri(jSONObject.optString("permission", null));
    }

    public int hashCode() {
        int i = 0;
        int hashCode = ((this.e == null ? 0 : this.e.hashCode()) + 31) * 31;
        if (this.f != null) {
            i = this.f.hashCode();
        }
        return hashCode + i;
    }

    public Scope[] getScopeArray() {
        return (Scope[]) this.e.toArray(new Scope[this.e.size()]);
    }
}
